/**
*B 级防雷器 js
*/ 

var	bathunderDS;
var bathunderFormPanel;
function doBathunderDelete(id)
{
	Ext.MessageBox.confirm('确认删除', '确认删除该信息?', 
	function(btn) {
		if(btn == 'yes') // 选中了是按钮	
		{
			JBathunderWeb.deleteBathunder(id,function(data)
			{
				if(data)
				{ 
					Ext.Msg.alert('成功',"操作成功！"); 
					bathunderDS.load({params:{start:0,limit:17,'<%=Constant.BTS_CODE%>':'<%=strbtscode%>'}});	 
					//bathunderFormPanel.getForm().reset();			
				}else
				{
					Ext.Msg.alert("失败","操作失败!");
				}
			})
		}
	})	
}


Ext.onReady(function(){  
	Ext.QuickTips.init();
	Ext.form.Field.prototype.msgTarget = 'side';

	var bathunderType = Ext.data.Record.create([								
							   {name: 'id', type: "string"},
							   {name: 'btscode', type: "string"},
							   {name: 'btsname', type: "string"},
							   {name: 'manufacturer', type: "string"},
							   {name: 'type', type: "string"}, 
							   {name: 'capacity', type: "string"}, 
							   {name: 'num', type: "string"}, 
							   {name: 'opentime', type: "date"},
							   {name: 'maintenance', type: 'string'},
							   {name: 'refreshtime',  type: 'date'},
							   {name: 'memo',  type: 'string'},
							   {name: 'hunderPowerType',  type: 'string'},
							   {name: 'hunderWorkSta',  type: 'string'},
							   {name: 'hunderLineLen',  type: 'string'}
						 ]);
	var bathunderCM = new Ext.grid.ColumnModel(
			[
				new Ext.grid.CheckboxSelectionModel({singleSelect: true}), 	            
				{header: "流水号", sortable: true, dataIndex: 'id',align:'left'},
				{header: "基站编号", sortable: true, dataIndex: 'btscode',align:'left'},
				{header: "基站名称",  sortable: true, dataIndex: 'btsname',align:'left'},	
				{header: "规格型号",  sortable: true, dataIndex: 'type',align:'left'},	
				{header: "数量",  sortable: true, dataIndex: 'num',align:'left'},	
				{header: "厂家",  sortable: true, dataIndex: 'manufacturer',align:'left'},	    
				{header: "启用日期",  width: 150,sortable: true, renderer: Ext.util.Format.dateRenderer('Y-m-d'),dataIndex: 'opentime',align:'left'},	 
				{header: "删除", dataIndex: 'id',renderer: showDelete}					
			]
		);				 
	
	function showDelete(id)
	{
		return "<input type='button' value='删除' class='x-btn button' onclick='doBathunderDelete(\"" + id  +   "\")'>";  
	}
	
	function formatFence(value) 
	{
		var strState = "";
		if(value == "0")
			strState = '<span style="color:red;">' + "否" + '</span>';
		else  
			strState = '是'; 		
		return strState;
	}
	
	bathunderDS = new Ext.data.Store({
				proxy: new Ext.data.DWRProxy(JBathunderWeb.getBathunderList, true),	
				reader: new Ext.data.ListRangeReader({
					totalProperty: 'totalSize',
					id: 'id'
				}, bathunderType),
				sortInfo:{field:'id', direction:'ASC'}// 排序信息
			});

	
	var towerMountedGridDiv = Ext.get("mainTabPanelDIV");
	var bathunderGrid = new Ext.grid.GridPanel({			 	
			ds: bathunderDS,
			cm: bathunderCM, 
			sm: new Ext.grid.RowSelectionModel({
                    singleSelect: true,
                    listeners: {
                        rowselect: function(sm, row, rec) {
                            bathunderFormPanel.getForm().loadRecord(rec);
                        }
                    }
                }),
			autoWidth : true,  
			autoScroll : true,  
			autoHeight　: false,
			region : 'center',
			height: 400,
			minSize: 200,		
			frame:true,
			collapsible:false, 
			enableDragDrop:false, 		
			bbar: new Ext.PagingToolbar({
					pageSize: 17,
					store: bathunderDS,
					displayInfo: true,
					displayMsg: '显示第 {0} 条到 {1} 条记录，一共 {2} 条',
					emptyMsg: "没有记录"
				})
		});
	bathunderDS.on('load', function() {
		bathunderGrid.getSelectionModel().selectFirstRow(); 
	});	

		
		
	var aircPowerTypeStore = new Ext.data.Store({  
		        proxy:new Ext.data.DWRProxy(JCommWeb.getDirctoryByType,{}),  
		        reader:new Ext.data.DWRArrayReader(
						{},
						new Ext.data.Record.create([  
							{name: 'text', mapping:1},       
							{name: 'value',mapping:0}                   
						  ])
						)  
		    }); 
	
	var aircPowerTypeCombo = new Ext.form.ComboBox({	   
			fieldLabel: '电源类型',
			name: 'aircPowerType',	
			width:150,
			valueField :"value",
			displayField: "text",
			mode: 'remote',
			selectOnFocus:true,                    
			forceSelection: true,
			//blankText:'请选电源类型',
			emptyText:'选择电源类型',
			hiddenName:'aircPowerType',
			triggerAction: 'all',
			//allowBlank:false,
			editable:false, 
			store: aircPowerTypeStore
		});	
	
	aircPowerTypeStore.on('beforeload', function() {
			  aircPowerTypeStore.baseParams = {
					DIRECTORY_TYPE:'24'   
			  };
		});	
	aircPowerTypeStore.load({params:{DIRECTORY_TYPE:'24'}});
	
	var aircTypeStore = new Ext.data.Store({  
		        proxy:new Ext.data.DWRProxy(JCommWeb.getDirctoryByType,{}),  
		        reader:new Ext.data.DWRArrayReader(
						{},
						new Ext.data.Record.create([  
							{name: 'text', mapping:1},       
							{name: 'value',mapping:0}                   
						  ])
						)  
		    }); 
	
	var aircTypeCombo = new Ext.form.ComboBox({	   
			fieldLabel: '空调类型',
			name: 'aircType',	
			width:150,
			valueField :"value",
			displayField: "text",
			mode: 'remote',
			selectOnFocus:true,                    
			forceSelection: true,
			//blankText:'请选电源类型',
			//emptyText:'aircType',
			hiddenName:'aircType',
			triggerAction: 'all',
			//allowBlank:false,
			editable:false, 
			store: aircTypeStore
		});	
	
	aircTypeStore.on('beforeload', function() {
			  aircTypeStore.baseParams = {
					DIRECTORY_TYPE:'27'   
			  };
		});	
	aircTypeStore.load({params:{DIRECTORY_TYPE:'27'}});
	
	var typeStore = new Ext.data.Store({  
		        proxy:new Ext.data.DWRProxy(JCommWeb.getDirctoryByType,{}),  
		        reader:new Ext.data.DWRArrayReader(
						{},
						new Ext.data.Record.create([  
							{name: 'text', mapping:1},       
							{name: 'value',mapping:0}                   
						  ])
						)  
		    }); 
	        
	var hunderPowerTypeStore  = new Ext.data.Store({  
		        proxy:new Ext.data.DWRProxy(JCommWeb.getDirctoryByType,{}),  
		        reader:new Ext.data.DWRArrayReader(
						{},
						new Ext.data.Record.create([  
							{name: 'text', mapping:1},       
							{name: 'value',mapping:0}                   
						  ])
						)  
		    }); 
	
	var hunderPowerTypeCombo = new Ext.form.ComboBox({	   
		fieldLabel: '电源类型',
		name: 'hunderPowerType',	
		width:150,
		valueField :"value",
	    displayField: "text",
	    mode: 'local',
		selectOnFocus:true,                    
		forceSelection: true, 
		hiddenName:'hunderPowerType',
		triggerAction: 'all', 
		editable:false, 
		store: hunderPowerTypeStore
	});
	hunderPowerTypeStore.on('beforeload', function() {
			  hunderPowerTypeStore.baseParams = {
					DIRECTORY_TYPE:'24'   
			  };
		});	
	hunderPowerTypeStore.load({params:{DIRECTORY_TYPE:'24'}});
	
	
	var hunderWorkStaStore  = new Ext.data.Store({  
			proxy:new Ext.data.DWRProxy(JCommWeb.getDirctoryByType,{}),  
			reader:new Ext.data.DWRArrayReader(
					{},
					new Ext.data.Record.create([  
						{name: 'text', mapping:1},       
						{name: 'value',mapping:0}                   
					  ])
					)  
		});
	var hunderWorkStaCombo = new Ext.form.ComboBox({	   
		fieldLabel: '工作状态',
		name: 'hunderWorkSta',	
		width:150,
		valueField :"value",
	    displayField: "text",
	    mode: 'local',
		selectOnFocus:true,                    
		forceSelection: true, 
		hiddenName:'hunderWorkSta',
		triggerAction: 'all', 
		editable:false, 
		store: hunderWorkStaStore
	});
	hunderWorkStaStore.on('beforeload', function() {
			  hunderWorkStaStore.baseParams = {
					DIRECTORY_TYPE:'26'   
			  };
		});	
	hunderWorkStaStore.load({params:{DIRECTORY_TYPE:'26'}});
	
	
	
	bathunderFormPanel = new Ext.form.FormPanel({
		region     : 'south', 
		bodyStyle  : 'padding: 10px; background-color: #DFE8F6',
		labelWidth : 130,  
		height: 190,
		minSize: 200,
		maxSize: 200,
		frame:true,
		buttonAlign:'right',
		layout:'column', 
		items:[
			 {
				columnWidth:.33,
				layout: 'form',  
				labelAlign:'right',	
				labelWidth:100,	 
				items: [
					{
						xtype:'hidden',
						fieldLabel: '流水号',
						width:150,
						name: 'id'
					}, {
						xtype:'textfield',
						readOnly: true,
						fieldLabel: '基站编号',
						width:150,
						name: 'btscode'
					}, {
						xtype:'textfield',
						fieldLabel: '基站名称',
						width:150,
						readOnly: true,
						name: 'btsname'
					}, {
						xtype:'numberfield',
						fieldLabel: '数量',
						width:150,
						name: 'num',
						value:1
					}, {
						xtype:'datefield',
						fieldLabel: '启用时间',
						width:150,
						format : "Y-m-d",
						allowBlank:false,
						name: 'opentime'
					}
					]
			 },
			 {
				columnWidth:.33,
				layout: 'form',  
				labelAlign:'right',	
				labelWidth:110,	 
				items: [
					{
						xtype:'textfield',
						fieldLabel: '规格型号',
						width:150,
						name: 'type'
					}, {
						xtype:'textfield',
						fieldLabel: '厂家',
						width:150,
						name: 'manufacturer'
					}
					, {
						xtype:'textfield',
						fieldLabel: '容量',
						width:150,
						value:1,
						name: 'capacity'
					}
					, {
						xtype:'textfield',
						fieldLabel: '更换及维修记录',
						width:150,
						name: 'maintenance'
					}]
			 },
			 {
				columnWidth:.33,
				layout: 'form',  
				labelAlign:'right',	
				labelWidth:110,	 
				items: [
					 hunderPowerTypeCombo
					,hunderWorkStaCombo
					,{
						xtype:'textfield',
						fieldLabel: '电缆到接地点长度',
						width:150,
						name: 'hunderLineLen'
					}
					,{
						xtype:'textarea',
						fieldLabel: '备注',
						width:150,
						name: 'memo'
					}]
			 }
			],
		buttons :[
			{
				text : ' 保存',
				handler : saveBathunderBO
			},{
				text : ' 新建/取消',
				handler : function(){
					bathunderFormPanel.getForm().reset(); 
					bathunderFormPanel.getForm().loadRecord(recordForAll);	
				}
			}
			]	
	});



	//Simple 'border layout' panel to house both grids
	
	var displayPanel = new Ext.Panel({
		width    : towerMountedGridDiv.getComputedWidth(),
		height   : 500,
		layout   : 'border',
		renderTo : 'BathundeTab',
		items    : [
			bathunderGrid,
			bathunderFormPanel
		]
	});
	
	
	function saveBathunderBO()
	{
		if (!bathunderFormPanel.getForm().isValid())
			return;	
			
		var form = bathunderFormPanel.getForm().getValues(false);	
		BathunderBO = {};
		BathunderBO.id = form.id;
		BathunderBO.btscode = form.btscode;
		BathunderBO.btsname= form.btsname;
		BathunderBO.type = form.type;
		BathunderBO.manufacturer = form.manufacturer;
		BathunderBO.capacity = form.capacity;
		BathunderBO.num = form.num;
		BathunderBO.maintenance = form.maintenance;
		BathunderBO.memo = form.memo; 		
		BathunderBO.hunderPowerType = form.hunderPowerType;		
		BathunderBO.hunderWorkSta = form.hunderWorkSta;
		BathunderBO.hunderLineLen = form.hunderLineLen; 
		
		var opentimeDate = new Date();	
		var arrPlanTime = form.opentime.split("-");		
				
		var yearOth = arrPlanTime[0];
		var monthOth = arrPlanTime[1];
		var dayOth = arrPlanTime[2];
		opentimeDate.setFullYear(yearOth,monthOth-1,dayOth);
		
		BathunderBO.opentime = opentimeDate;
	   			
		JBathunderWeb.saveBathunder(BathunderBO,function(data)
		{
			if(data)
			{ 
				Ext.Msg.alert('成功',"操作成功！"); 
				loadDate(bathunderDS,bathunderFormPanel);		 
			}else
			{
				Ext.Msg.alert("失败","操作失败!");
			}
		})
			
	}
	loadDate(bathunderDS,bathunderFormPanel);		
	
})
